package cn.itlaobing.web.controller.management;

import cn.itlaobing.web.entity.SystemRole;
import cn.itlaobing.web.entity.SystemUser;
import cn.itlaobing.web.service.ISystemUserService;
import cn.itlaobing.web.utils.AssignpermissionsUtil;
import cn.itlaobing.web.utils.IConstants;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;

@Controller
public class SystemUserController extends BaseManagementController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISystemUserService userService;



    @RequestMapping(value = "checkUserName")
    @ResponseBody
    public JSONObject checkUsername(@RequestParam(value = "userAccount") String userAccount) {
        JSONObject json = new JSONObject();
        SystemUser user = userService.findByUserAccount(userAccount);

        //判断对象user是否存在
        if ("".equals(user) || user == null) {
            json.put("result", true);
        } else {
            json.put("result", false);
        }
        return json;
    }

    /**
     * 登录
     * @param model
     * @param request
     * @param user
     * @return
     */
    @RequestMapping(value = "dologin")
    public String doLogin(Model model, HttpServletRequest request,SystemUser user) {

        //判断是否登录
        if(user.getUserAccount()==null || user.getUserPass()==null){
            return "redirect:/management/login";
        }

        SystemUser su = userService.findByUserAccountAndUserPass(user.getUserAccount(), user.getUserPass());
        //判断返回对象是否存在，如不存在提示信息和用户账号和密码回显回去
        if (su == null) {
            model.addAttribute("user", user);
            model.addAttribute("notice", "用户名或密码不正确");
            return "management/login";
        }


        //分配权限类
        AssignpermissionsUtil assign = new AssignpermissionsUtil();
        SystemUser suser =  assign.assignPer(su);
        //记录session
        request.getSession().setAttribute(IConstants.MANAGEMENTLOGIN,suser);
        //修改上次登录时间,登录状态
        Date date = new Date();
        Timestamp time = new Timestamp(date.getTime());
        int islogin = 1;
        userService.updateLoginById(suser.getId(),time,islogin);

        return "management/index";
    }

    //登出
    @RequestMapping("logout")
    public String logout(HttpServletRequest request){
        SystemUser user = (SystemUser) request.getSession().getAttribute(IConstants.MANAGEMENTLOGIN);
        Date date = new Date();
        Timestamp time = new Timestamp(date.getTime());
        int islogin = 0;
        userService.updateLoginById(user.getId(),time,islogin);
        request.getSession().removeAttribute(IConstants.MANAGEMENTLOGIN);
        return "management/login";
    }

    /**
     * 修改头像
     * @param headimg
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping(value = "uploadimg")
    public String uploadimg(@RequestParam(value = "headimg") String headimg,HttpServletRequest request){
        SystemUser user = (SystemUser) request.getSession().getAttribute(IConstants.MANAGEMENTLOGIN);
        SystemUser suser = userService.updateHeadImgById(user.getId(),headimg);
        if(suser!=null){
            //分配权限类
            AssignpermissionsUtil assign = new AssignpermissionsUtil();
            SystemUser su =  assign.assignPer(suser);
            request.getSession().setAttribute(IConstants.MANAGEMENTLOGIN,su);
        }
        return "management/index";
    }

    @RequestMapping(value = "checkOldPass")
    @ResponseBody
    public JSONObject checkOldPass(HttpServletRequest request,@RequestParam(value = "oldpass") String oldpass) {
        JSONObject json = new JSONObject();
        SystemUser suser = (SystemUser) request.getSession().getAttribute(IConstants.MANAGEMENTLOGIN);
        SystemUser user = userService.findByUserAccountAndUserPass(suser.getUserAccount(),oldpass);
        //判断对象user是否存在
        if ("".equals(user) || user == null) {
            json.put("result", true);
        } else {
            json.put("result", false);
        }
        return json;
    }

    /**
     * 修改密码
     * @param request
     * @param newpass
     * @return
     */
    @Transactional
    @RequestMapping(value = "updatepass")
    @ResponseBody
    public JSONObject updatepass(@RequestParam(value = "newpass") String newpass,HttpServletRequest request){
        JSONObject json = new JSONObject();
        SystemUser user = (SystemUser) request.getSession().getAttribute(IConstants.MANAGEMENTLOGIN);
        SystemUser suser = userService.updatePassById(user.getId(),newpass);
        if(suser!=null){
            //分配权限类
            AssignpermissionsUtil assign = new AssignpermissionsUtil();
            SystemUser su =  assign.assignPer(suser);
            request.getSession().setAttribute(IConstants.MANAGEMENTLOGIN,su);
            json.put("result",true);
        }
        return json;
    }

    /**
     * 用户列表
     * @return
     */
    @RequestMapping(value = "userList")
    public String userList(){

        return "management/userList";
    }
}
